﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine.EventSystems;
using UnityEngine.UIElements;

namespace Miao.UI
{
    public abstract class DragAndDropEventT<T> : DragAndDrop
    {
        public T Data;
        public bool dragin { get; set; } = false;
         public int beforeIndex { get; set; } = -1;
        /// <summary>
        /// 绑定父类的序号
        /// </summary>
        public int index
        {
            get
            {
                if (!dragin)
                {
                    return this.transform.parent.GetSiblingIndex();
                }
                else
                {
                    return beforeIndex;
                }
            }
        }
        public event Action<DragAndDropEventT<T>, PointerEventData> onBeginDrag;
        public event Action<DragAndDropEventT<T>, PointerEventData> onDrag;
        public event Action<DragAndDropEventT<T>, PointerEventData> onEndDrag;

        public abstract void Init();
        public virtual void Refresh()
        {
            
        }
        public override void OnBeginDrag(PointerEventData eventData)
        {
            beforeIndex = this.transform.parent.GetSiblingIndex();
            dragin = true;
            onBeginDrag?.Invoke(this, eventData);
            base.OnBeginDrag(eventData);
        }
        public override void OnDrag(PointerEventData eventData)
        {
            onDrag?.Invoke(this, eventData);
            base.OnDrag(eventData);
        }

        public override void OnEndDrag(PointerEventData eventData)
        {
            onEndDrag?.Invoke(this, eventData);
            base.OnEndDrag(eventData);
            beforeIndex = -1;
            dragin = false;
        }
    }
}
